/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

/**
 * Utility methods for databinding.
 * 
 * @author Brad Reynolds
 */
public class Databinding {
    /**
     * Binding namespace.
     */
    public static final String BINDING = "binding"; //$NON-NLS-1$

    /**
     * Target namespace.
     */
    public static final String TARGET = "target"; //$NON-NLS-1$

    public static final String DATABINDING = "databinding"; //$NON-NLS-1$
    
    public static final String PROPERTY_NAME = "property_name"; //$NON-NLS-1$
    
    /**
     * Names of event types.
     */
    private static final String[] EVENT_NAMES = new String[] { "EVENT_COPY_TO_TARGET", "EVENT_COPY_TO_MODEL", //$NON-NLS-1$ //$NON-NLS-2$
            "EVENT_PARTIAL_VALIDATE", "EVENT_REMOVE", "EVENT_LAZY_INSERT", "EVENT_LAZY_DELETE" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

    /**
     * Names of pipeline positions.
     */
    private static final String[] PIPELINE_POSITION_NAMES = new String[] { "PIPELINE_AFTER_GET", //$NON-NLS-1$
            "PIPELINE_AFTER_VALIDATE", "PIPELINE_AFTER_CONVERT", "PIPELINE_AFTER_BUSINESS_VALIDATE",  //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
            "PIPELINE_AFTER_CHANGE", "PIPELINE_VALIDATION_ERROR_OR_WARNING" };  //$NON-NLS-1$//$NON-NLS-2$

    /**
     * Translates the event to its name.
     * 
     * @param bindingEvent
     * @return name of the binding event
     */
    public static String getBindingEventName(int bindingEvent) {
        checkIndex(bindingEvent, EVENT_NAMES);
        
        return EVENT_NAMES[bindingEvent];
    }

    /**
     * Translates the position to its name.
     * 
     * @param pipelinePosition
     * @return name of the pipeline position
     */
    public static String getPipelinePositionName(int pipelinePosition) {
        checkIndex(pipelinePosition, PIPELINE_POSITION_NAMES);
        
        return PIPELINE_POSITION_NAMES[pipelinePosition];
    }
    
    /**
     * Validates the index against an array.
     * 
     * @param index
     * @param array
     */
    private static void checkIndex(int index, String[] array) {
        if (index >= array.length || index < 0) {
            throw new IllegalArgumentException("Parameter index [" + index + "] is out of bounds."); //$NON-NLS-1$//$NON-NLS-2$
        }
    }
}